import { get, post } from 'utils/request.js';
import { config } from 'config.js';


// 获取应用实例
let configs = config();
const app = getApp(); //  eslint-disable-line no-undef
const delay = (t = 0) => new Promise((resolve) => setTimeout(resolve, t))
Page({
  data: {
    form: {
      id: '',
      name: '',
      questions: [],
      questionsArr: [],
      index: 0,
      content: '',
      showImgUrl: '',
      question: ''
    },
    upload: {
      imgUrl: require('../../images/complaint/icon-picture.svg'),
      showImgUrl: require('../../images/complaint/loading.gif'),
      isShow: true
    },
    iconError: require('../../images/com/error.svg')

    
  },
  async onLoad(options) {
    var id = options.id || 0;
    var that = this;

    that.setData({
      'form.id': id,
      'form.name': options.name || '234'
    })

    get({'path': configs.complainInit}, {}).then(function(res){
      var tempData = res.data;
      if (tempData.ret == 0) {

        var data = tempData.data || [];
        var questionsArr = [];
        data.map(function(el, index){
           el.question && questionsArr.push(el.question);
        })

        that.setData({
          'form.questionsArr': questionsArr,
          'form.questions': data
        })

      } else {
        wx.showToast({
          title: ratingData.msg,
          image: that.data.iconError
        })
        return false;
      }
    }, function(res){
      wx.showToast({
        title: res.data.msg,
        image: that.data.iconError
      })

      return false;
    })

    // 调用应用实例的方法获取全局数
    
  },
  bindPickerChange (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var index = e.detail.value;
    this.setData({
      'form.index': index,
      'form.question': this.data.form.questions[index].qid
    })
  },
  async uploadCallback (e) {
    var that = this;
    that.setData({
      'upload.isShow': false
    })


    var imgUrl = e.detail && ( e.detail.data.image_url || '' );
    await delay(500).then(function(){
      if(imgUrl){
        that.setData({
          'upload.showImgUrl': imgUrl,
          'form.showImgUrl': imgUrl
        })
      }else{
        that.setData({
          'upload.isShow': true
        })
      }
    });
  },
  formSubmit(e){
    var that = this;
    var data = e.detail.value;
    if(data['name'] == ''){
      wx.showToast({
        title: '投诉对象必填',
        image: that.data.iconError
      })
      return false;
    }

    if(data['question'] == ''){
      wx.showToast({
        title: '投诉问题必选',
        image: that.data.iconError
      })
      return false;
    }
    if(data['content'] == ''){
      wx.showToast({
        title: '投诉原因必填',
        image: that.data.iconError
      })
      return false;
    }

    post({'path': configs.complainAdd}, data).then(function(res){
      var ratingData = res.data;
      if (ratingData.ret == 0) {
        
        // 投诉完成后返回
        //登陆跳转到首页
        wx.navigateTo({
          url: '/pages/complaint-list/complaint-list'
        })

      } else {
        wx.showToast({
          image: require('../../images/com/error.svg'),
          title: ratingData.msg
        })
        return false;
      }
    }, function(res){
      wx.showToast({
        image: require('../../images/com/error.svg'),
        title: res.data.msg
      })
      return false;
    })
  },
  deleteImageTap(e){
    this.setData({
      'upload.isShow': true,
      'upload.showImgUrl': require('../../images/complaint/loading.gif'),
      'upload.imgUrl': require('../../images/complaint/icon-picture.svg'),
      'form.showImgUrl': ''
    })
  },
  previewImageTap(e){
    var imgurl = e.target.dataset.imgurl;
    wx.previewImage({
      current: imgurl, // 当前显示图片的http链接
      urls: [imgurl] // 需要预览的图片http链接列表
    })
  },
  textareaChange(e){
    this.setData({
      'form.content': e.detail.value
    })
  }
});
